﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmjftop : Form
    {
        public frmjftop()
        {
            InitializeComponent();
        }
        DataSet ds;
        private void frmjftop_Load(object sender, EventArgs e)
        {
            ddldate.Text = "所有";
            dtps.Value = DateTime.Now;
            dtpe.Value = DateTime.Now;
            bind();

        }       
        private void btnOK_Click(object sender, EventArgs e)
        {
            bind();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddldate.Text == "之间")
            {
                dtps.Visible = true;
                dtpe.Visible = true;
            }
            else if (ddldate.Text == "所有")
            {
                dtps.Visible = false;
                dtpe.Visible = false;
            }
            else
            {
                dtps.Visible = true;
                dtpe.Visible = false;
            }
        }
        private void bind()
        {
            dgvin.AutoGenerateColumns = false;
            string sql;
            sql = "SELECT customer.cname, sum(jifenconfig.jnum) as jsum FROM jifenconfig RIGHT JOIN (jifeninfo LEFT JOIN customer ON jifeninfo.cid = customer.cid) ON jifenconfig.ID = jifeninfo.jid where 1=1  ";
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and jdate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and jdate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "小于")
            {
                sql += string.Format(" and jdate<#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "等于")
            {
                sql += string.Format(" and jdate =#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "之间")
            {
                sql += string.Format(" and jdate>#{0}# and jdate<#{1}#", dtps.Value.ToShortDateString(), dtpe.Value.ToShortDateString());
            }
            sql += "  group by customer.cname  order by sum(jifenconfig.jnum) desc";
            ds = DbHelperOleDb.Query(sql);           
            dgvin.DataSource = ds.Tables[0];
            int widths = 0;
            for (int i = 0; i < dgvin.Columns.Count; i++)
            {
                dgvin.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader);  // 自动调整列宽  
                widths += dgvin.Columns[i].Width;   // 计算调整列后单元列的宽度和                       
            }
            if (widths >= dgvin.Size.Width)  // 如果调整列的宽度大于设定列宽  
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;  // 调整列的模式 自动  
            else
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  // 如果小于 则填充  
        }


        private void btnOut_Click(object sender, EventArgs e)
        {
            myfun.DataToExcel(dgvin);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvin);
            m.Print();
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            
            string sql = "delete from jifeninfo where 1=1 ";
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and jdate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and jdate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "小于")
            {
                sql += string.Format(" and jdate<#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "等于")
            {
                sql += string.Format(" and jdate =#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "之间")
            {
                sql += string.Format(" and jdate>#{0}# and jdate<#{1}#", dtps.Value.ToShortDateString(), dtpe.Value.ToShortDateString());
            }
            if (MessageBox.Show("您真的要清除所选日期的积分吗？","提示",MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DbHelperOleDb.ExecuteSql(sql);
                bind();
            }
           
        }

        private void dgvin_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, new SolidBrush(Color.CadetBlue), e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加行号时发生错误，错误信息：" + ex.Message, "操作失败");
            }
        }
    }
}
